package com.dayu.finecomm.protocol.log;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

/**
 * @ClassName: RawlLogger
 * @Description:报文打印
 * @author: 戴卫国
 * @date: 2018年4月4日 上午11:21:55
 * 
 * @Copyright: 2018 dayu Inc. All rights reserved.
 */
public class RawLogger extends ByteToMessageDecoder {

	boolean validPacket;

	Logger LOGGER = LoggerFactory.getLogger(getClass());

	private static final char[] TBL = new char[256 * 4];

	static {

		final char[] DIGITS = "0123456789ABCDEF".toCharArray();

		for (int i = 0; i < 256; i++) {
			TBL[i << 1] = DIGITS[i >>> 4 & 0x0F];
			TBL[(i << 1) + 1] = DIGITS[i & 0x0F];
		}
	}

	public RawLogger() {
	}

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer(in.readableBytes() * 3);

		while (in.isReadable()) {

			int i = in.readUnsignedByte() << 1;
			sb.append(TBL[i]).append(TBL[i + 1]);
			sb.append(" ");
		}

		LOGGER.info(sb.toString());
		ctx.fireChannelRead(in);
	}
}
